package com.cloudptt.wp.ui.views;

import com.cloudptt.api.comm.Const;
import com.cloudptt.api.product.adapter.GCallEventAdapter;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.R;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.view.View.*;


public class PTTLayout extends LinearLayout implements OnClickListener, OnTouchListener{

	public static final String TAG = "PTTLayout";

	private LinearLayout mPttLayout;
//	private ImageView mPttImageView;
	private ImageView mCustomImageView;
	private ImageView mWarningImageView;
	private ImageView mHangupImageView;

	private AnimationDrawable animationDrawable;

	private GCallEventAdapter mCallEventAdapter = null;
	
	private CustomListener customListener;

	public PTTLayout(Context context) {
		this(context, null);
		// TODO Auto-generated constructor stub
	}

	public PTTLayout(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		// TODO Auto-generated constructor stub
	}

	public PTTLayout(Context context, AttributeSet attrs, int defStyleAttr) {
		this(context, attrs, defStyleAttr, 0);
		// TODO Auto-generated constructor stub
	}

	public PTTLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
		super(context, attrs, defStyleAttr, defStyleRes);
		// TODO Auto-generated constructor stub

		LayoutInflater.from(context).inflate(R.layout.ptt_layout, this);

		mPttLayout = (LinearLayout)findViewById(R.id.ptt_layout);
//		mPttImageView = (ImageView)findViewById(R.id.group_ptt_imageview);
		mCustomImageView = (ImageView)findViewById(R.id.custom_iv);
		mWarningImageView = (ImageView)findViewById(R.id.warning_imageview);
		mHangupImageView = (ImageView)findViewById(R.id.hangup_imageview);
	}

	public void setCallAdapter(GCallEventAdapter callEventAdapter){
		mCallEventAdapter = callEventAdapter;
	}

	private void updatePttUI(){
//		if(mPttImageView != null){
//			//mPttImageView.setPressed(true);
//			mPttImageView.setImageResource(R.drawable.icon_group_ptt_pressed);
//		}

//		if(mPttImageView != null){
//			animationDrawable = (AnimationDrawable)mPttImageView.getDrawable();
//			animationDrawable.start();
//		}
		//				showCallStatusTv(true);
		if(mCallEventAdapter != null){
			mCallEventAdapter.onPtt(Const.MSG_TALK_ON, true,false,true,false);
		}
	}
	public void onStop()
	{
		Log.d(TAG, "onStop");
		mCustomImageView.setOnClickListener(null);
		mPttLayout.setOnTouchListener(null);
		mHangupImageView.setOnClickListener(null);
	}
	public void onResume()
	{
		Log.d(TAG, "onResume");
			mCustomImageView.setOnClickListener(this);
			mPttLayout.setOnTouchListener(this);
			mHangupImageView.setOnClickListener(this);
	}

	private void doPttOff(boolean isMust){
		Log.d(TAG, "doPttOff");
		if(animationDrawable != null){
			animationDrawable.stop();
		}
//		if(mPttImageView != null){
//			mPttImageView.setPressed(false);
//			mPttImageView.setImageResource(R.drawable.icon_group_ptt);
//		}
		//				showCallStatusTv(false);
		if(mCallEventAdapter != null){
			mCallEventAdapter.onPtt(Const.MSG_TALK_OFF, true,false,true,false);
		}
	}

	private void pressPTT(boolean usingPtt){
		if(usingPtt){
			updatePttUI();
		}
	}
	
	public void setCustomView(int resId, CustomListener listener){
		mCustomImageView.setImageResource(resId);
		customListener = listener;
	}
	
	public void showHangup(boolean isShow){
		if(isShow){
			if(mHangupImageView != null 
					&& !mHangupImageView.isShown()){
				mHangupImageView.setVisibility(View.GONE);
			}
		}else{
			if(mHangupImageView != null
					&& mHangupImageView.isShown()){
				mHangupImageView.setVisibility(View.GONE);
			}
		}
	}
	
	public void clearState(){
		mHangupImageView.setVisibility(View.GONE);
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		if(R.id.hangup_imageview == view.getId()){
			if(mCallEventAdapter != null){
				mCallEventAdapter.hangUpGroupCall();
			}
		}
		
		else if(R.id.custom_iv == view.getId()){
			Log.d(TAG, "click custom");
			if(customListener != null)
			{
				customListener.customFunc();
			}
		}
		
	}

	@Override
	public boolean onTouch(View view, MotionEvent event) {
		// TODO Auto-generated method stub
		Log.d(TAG, "event action = "+event.getAction());
		if(view.getId() == R.id.ptt_layout){
			if(event.getAction() == MotionEvent.ACTION_DOWN){
				pressPTT(true);
				return true;
			}else if(event.getAction() == MotionEvent.ACTION_UP){
				doPttOff(false);
				return true;
			}
		}
		return false;
	}

}
